home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.3 / Printer / src / EpsonQ / render.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  7.1 KB  |  260 lines

  1. /*
  2.     EpsonQ (LQ-800/LQ-850/LQ-1000/LQ-1050/LQ-1500/LQ-2500) driver.
  3.     (tested on a Star NB24-15 (bw) and an Epson LQ-2500 (color) printer).
  4.     David Berezowski - October/87.
  5.  
  6.   Copyright (c) 1988 Commodore-Amiga, Inc.
  7.  
  8.   Executables based on this information may be used in software
  9.   for Commodore Amiga computers.  All other rights reserved.
  10.  
  11.   This information is provided "as is"; no warranties are made.
  12.   All use is at your own risk, and no liability or responsibility is assumed.
  13. */
  14.  
  15.  
  16. #include <exec/types.h>
  17. #include <exec/nodes.h>
  18. #include <exec/lists.h>
  19. #include <exec/memory.h>
  20. #include "../printer/printer.h"
  21. #include "../printer/prtbase.h"
  22. #include "../printer/prtgfx.h"
  23.  
  24. #define NUMSTARTCMD    8    /* # of cmd bytes before binary data */
  25. #define NUMENDCMD    1    /* # of cmd bytes after binary data */
  26. #define NUMTOTALCMD (NUMSTARTCMD + NUMENDCMD)    /* total of above */
  27. #define NUMLFCMD    4    /* # of cmd bytes for linefeed */
  28. #define MAXCOLORBUFS    4    /* max # of color buffers */
  29.  
  30. #define STARTLEN    16
  31. #define PITCH        1
  32. #define CONDENSED    2
  33. #define LMARG        8
  34. #define RMARG        11
  35. #define DIREC        15
  36.  
  37. Render(ct, x, y, status)
  38. long ct, x, y, status;
  39. {
  40.     extern void *AllocMem(), FreeMem();
  41.  
  42.     extern struct PrinterData *PD;
  43.     extern struct PrinterExtendedData *PED;
  44.  
  45.     static UWORD RowSize, ColorSize, BufSize, TotalBufSize;
  46.     static UWORD dataoffset, dpi_code;
  47.     static UWORD colors[MAXCOLORBUFS]; /* color ptrs */
  48.     static UWORD colorcodes[MAXCOLORBUFS]; /* printer color codes */
  49.     static UWORD NumColorBufs; /* actually number of color buffers req. */
  50.     /*
  51.         00-01    \003P        set pitch (10 or 12 cpi)
  52.         02-02    \022        set condensed fine (on or off)
  53.         03-05    \033W\000    enlarge off
  54.         06-08    \033ln        set left margin to n
  55.         09-11    \033Qn        set right margin to n
  56.         12-12    \015        carriage return
  57.         13-15    \033U1        set uni-directional mode
  58.     */
  59.     static UBYTE StartBuf[STARTLEN] =
  60.         "\033P\022\033W\000\033ln\033Qn\015\033U1";
  61.     UBYTE *ptr, *ptrstart, *ptr2, *ptr2start;
  62.     int i, err;
  63.  
  64.     switch(status) {
  65.         case 0:  /* Master Initialization */
  66.             /*
  67.                 ct    - pointer to IODRPReq structure.
  68.                 x    - width of printed picture in pixels.
  69.                 y    - height of printed picture in pixels.
  70.             */
  71.             RowSize = x * 3;
  72.             ColorSize = RowSize + NUMTOTALCMD;
  73.             if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  74.                 NumColorBufs = MAXCOLORBUFS;
  75.                 colors[0] = ColorSize * 3; /* Black */
  76.                 colors[1] = ColorSize * 0; /* Yellow */
  77.                 colors[2] = ColorSize * 1; /* Magenta */
  78.                 colors[3] = ColorSize * 2; /* Cyan */
  79.                 colorcodes[0] = 4; /* Yellow */
  80.                 colorcodes[1] = 1; /* Magenta */
  81.                 colorcodes[2] = 2; /* Cyan */
  82.                 colorcodes[3] = 0; /* Black */
  83.             }
  84.             else { /* grey-scale or black&white */
  85.                 NumColorBufs = 1;
  86.                 colors[0] = ColorSize * 0; /* Black */
  87.                 colorcodes[0] = 0; /* Black */
  88.             }
  89.             BufSize = ColorSize * NumColorBufs + NUMLFCMD;
  90.             TotalBufSize = BufSize * 2;
  91.             PD->pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
  92.             if (PD->pd_PrintBuf == NULL) {
  93.                 err = PDERR_BUFFERMEMORY; /* no mem */
  94.             }
  95.             else {
  96.                 dataoffset = NUMSTARTCMD;
  97.                 /*
  98.                     This printer prints graphics within its
  99.                     text margins.  This code makes sure the
  100.                     printer is in 10 cpi and then sets the
  101.                     left and right margins to their minimum
  102.                     and maximum values (respectively).  A
  103.                     carriage return is sent so that the
  104.                     print head is at the leftmost position
  105.                     as this printer starts printing from
  106.                     the print head's position.  The printer
  107.                     is put into unidirectional mode to
  108.                     reduce wavy vertical lines.
  109.                 */
  110.                 StartBuf[PITCH] = 'P'; /* 10 cpi */
  111.                 StartBuf[CONDENSED] = '\022'; /* off */
  112.                 /* left margin of 1 */
  113.                 StartBuf[LMARG] = 0;
  114.                 /* right margin of 80 or 136 */
  115.                 StartBuf[RMARG] = PD->pd_Preferences.
  116.                     PaperSize == W_TRACTOR ? 136 : 80;
  117.                 /* uni-directional mode */
  118.                 StartBuf[DIREC] = '1';
  119.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  120.             }
  121.             break;
  122.  
  123.         case 1: /* Scale, Dither and Render */
  124.             /*
  125.                 ct    - pointer to PrtInfo structure.
  126.                 x    - 0.
  127.                 y    - row # (0 to Height - 1).
  128.             */
  129.             Transfer(ct, y, &PD->pd_PrintBuf[dataoffset], colors);
  130.             err = PDERR_NOERR; /* all ok */
  131.             break;
  132.  
  133.         case 2: /* Dump Buffer to Printer */
  134.             /*
  135.                 ct    - 0.
  136.                 x    - 0.
  137.                 y    - # of rows sent (1 to NumRows).
  138.             */
  139.             /* white-space strip */
  140.             ptrstart = &PD->pd_PrintBuf[dataoffset];
  141.             ptr2start = ptr2 = ptrstart - NUMSTARTCMD;
  142.             x = 0; /* flag no transfer required yet */
  143.             for (ct=0; ct<NumColorBufs;
  144.                 ct++, ptrstart += ColorSize) {
  145.                 i = RowSize;
  146.                 ptr = ptrstart + i - 1;
  147.                 while (i > 0 && *ptr == 0) {
  148.                     i--;
  149.                     ptr--;
  150.                 }
  151.                 if (i != 0) { /* if data */
  152.                     /* convert to # of pixels */
  153.                     i = (i + 2) / 3;
  154.                     ptr = ptrstart - NUMSTARTCMD;
  155.                     *ptr++ = 27;
  156.                     *ptr++ = 'r';
  157.                     *ptr++ = colorcodes[ct]; /* color */
  158.                     *ptr++ = 27;
  159.                     *ptr++ = '*';
  160.                     *ptr++ = dpi_code;    /* density */
  161.                     *ptr++ = i & 0xff;
  162.                     *ptr++ = i >> 8;    /* size */
  163.                     i *= 3; /* back to # of bytes used */
  164.                     *(ptrstart + i) = 13; /* cr */
  165.                     i += NUMTOTALCMD;
  166.                     /* if must transfer data */
  167.                     if (x != 0) {
  168.                         /* get src start */
  169.                         ptr = ptrstart - NUMSTARTCMD;
  170.                         /* xfer and update dest ptr */
  171.                         do {
  172.                             *ptr2++ = *ptr++;
  173.                         } while (--i);
  174.                     }
  175.                     else { /* no transfer required */
  176.                         /* update dest ptr */
  177.                         ptr2 += i;
  178.                     }
  179.                 }
  180.                 /* if compacted or 0 */
  181.                 if (i != RowSize + NUMTOTALCMD) {
  182.                     /* we need to transfer next time */
  183.                     x = 1;
  184.                 }
  185.             }
  186.             *ptr2++ = 13; /* cr */
  187.             *ptr2++ = 27;
  188.             *ptr2++ = 'J';
  189.             *ptr2++ = y; /* y/180 lf */
  190.             err = (*(PD->pd_PWrite))(ptr2start, ptr2 - ptr2start);
  191.             if (err == PDERR_NOERR) {
  192.                 dataoffset = (dataoffset == NUMSTARTCMD ?
  193.                     BufSize : 0) + NUMSTARTCMD;
  194.             }
  195.             break;
  196.  
  197.         case 3: /* Clear and Init Buffer */
  198.             /*
  199.                 ct    - 0.
  200.                 x    - 0.
  201.                 y    - 0.
  202.             */
  203.             ptr = &PD->pd_PrintBuf[dataoffset];
  204.             i = BufSize - NUMTOTALCMD - NUMLFCMD;
  205.             do {
  206.                 *ptr++ = 0;
  207.             } while (--i);
  208.             err = PDERR_NOERR; /* all ok */
  209.             break;
  210.  
  211.         case 4: /* Close Down */
  212.             /*
  213.                 ct    - error code.
  214.                 x    - io_Special flag from IODRPReq.
  215.                 y    - 0.
  216.             */
  217.             err = PDERR_NOERR; /* assume all ok */
  218.             /* if user did not cancel print */
  219.             if (ct != PDERR_CANCEL) {
  220.                 /* restore preferences pitch and margins */
  221.                 if (PD->pd_Preferences.PrintPitch == ELITE) {
  222.                     StartBuf[PITCH] = 'M'; /* 12 cpi */
  223.                 }
  224.                 else if (PD->pd_Preferences.PrintPitch == FINE) {
  225.                     StartBuf[CONDENSED] = '\017'; /* on */
  226.                 }
  227.                 StartBuf[LMARG] =
  228.                     PD->pd_Preferences.PrintLeftMargin - 1;
  229.                 StartBuf[RMARG] =
  230.                     PD->pd_Preferences.PrintRightMargin;
  231.                 StartBuf[DIREC] = '0'; /* bi-directional */
  232.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  233.             }
  234.              /* wait for both buffers to empty */
  235.             (*(PD->pd_PBothReady))();
  236.             if (PD->pd_PrintBuf != NULL) {
  237.                 FreeMem(PD->pd_PrintBuf, TotalBufSize);
  238.             }
  239.             break;
  240.  
  241.         case 5:   /* Pre-Master Initialization */
  242.             /*
  243.                 ct    - 0 or pointer to IODRPReq structure.
  244.                 x    - io_Special flag from IODRPReq.
  245.                 y    - 0.
  246.             */
  247.             /*
  248.                 Kludge for weak power supplies.
  249.                 FANFOLD - use all 24 pins (default).
  250.                 SINGLE  - use only 16 pins.
  251.             */
  252.             PED->ped_NumRows = PD->pd_Preferences.PaperType ==
  253.                 SINGLE ? 16 : 24;
  254.             dpi_code = SetDensity(x & SPECIAL_DENSITYMASK);
  255.             err = PDERR_NOERR; /* all ok */
  256.             break;
  257.     }
  258.     return(err);
  259. }
  260.